/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MeteoCal.controller;

import MeteoCal.bean.exceptions.IllegalOrphanException;
import MeteoCal.bean.exceptions.NonexistentEntityException;
import MeteoCal.bean.exceptions.PreexistingEntityException;
import MeteoCal.bean.exceptions.RollbackFailureException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import MeteoCal.entity.Schedule;
import MeteoCal.entity.Warningevent;
import java.util.ArrayList;
import java.util.Collection;
import MeteoCal.entity.Receivednotification;
import MeteoCal.entity.Invitation;
import MeteoCal.entity.Warningcomment;
import MeteoCal.entity.Comment;
import MeteoCal.entity.Event;
import MeteoCal.entity.User;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;

/**
 *
 * @author Daniele
 */
public class UserJpaController implements Serializable {

    public UserJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(User user) throws PreexistingEntityException, RollbackFailureException, Exception {
        if (user.getWarningeventCollection() == null) {
            user.setWarningeventCollection(new ArrayList<Warningevent>());
        }
        if (user.getReceivednotificationCollection() == null) {
            user.setReceivednotificationCollection(new ArrayList<Receivednotification>());
        }
        if (user.getInvitationCollection() == null) {
            user.setInvitationCollection(new ArrayList<Invitation>());
        }
        if (user.getWarningcommentCollection() == null) {
            user.setWarningcommentCollection(new ArrayList<Warningcomment>());
        }
        if (user.getCommentCollection() == null) {
            user.setCommentCollection(new ArrayList<Comment>());
        }
        if (user.getEventCollection() == null) {
            user.setEventCollection(new ArrayList<Event>());
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Schedule idSchedule = user.getIdSchedule();
            if (idSchedule != null) {
                idSchedule = em.getReference(idSchedule.getClass(), idSchedule.getIdSchedule());
                user.setIdSchedule(idSchedule);
            }
            Collection<Warningevent> attachedWarningeventCollection = new ArrayList<Warningevent>();
            for (Warningevent warningeventCollectionWarningeventToAttach : user.getWarningeventCollection()) {
                warningeventCollectionWarningeventToAttach = em.getReference(warningeventCollectionWarningeventToAttach.getClass(), warningeventCollectionWarningeventToAttach.getWarningeventPK());
                attachedWarningeventCollection.add(warningeventCollectionWarningeventToAttach);
            }
            user.setWarningeventCollection(attachedWarningeventCollection);
            Collection<Receivednotification> attachedReceivednotificationCollection = new ArrayList<Receivednotification>();
            for (Receivednotification receivednotificationCollectionReceivednotificationToAttach : user.getReceivednotificationCollection()) {
                receivednotificationCollectionReceivednotificationToAttach = em.getReference(receivednotificationCollectionReceivednotificationToAttach.getClass(), receivednotificationCollectionReceivednotificationToAttach.getReceivednotificationPK());
                attachedReceivednotificationCollection.add(receivednotificationCollectionReceivednotificationToAttach);
            }
            user.setReceivednotificationCollection(attachedReceivednotificationCollection);
            Collection<Invitation> attachedInvitationCollection = new ArrayList<Invitation>();
            for (Invitation invitationCollectionInvitationToAttach : user.getInvitationCollection()) {
                invitationCollectionInvitationToAttach = em.getReference(invitationCollectionInvitationToAttach.getClass(), invitationCollectionInvitationToAttach.getInvitationPK());
                attachedInvitationCollection.add(invitationCollectionInvitationToAttach);
            }
            user.setInvitationCollection(attachedInvitationCollection);
            Collection<Warningcomment> attachedWarningcommentCollection = new ArrayList<Warningcomment>();
            for (Warningcomment warningcommentCollectionWarningcommentToAttach : user.getWarningcommentCollection()) {
                warningcommentCollectionWarningcommentToAttach = em.getReference(warningcommentCollectionWarningcommentToAttach.getClass(), warningcommentCollectionWarningcommentToAttach.getWarningcommentPK());
                attachedWarningcommentCollection.add(warningcommentCollectionWarningcommentToAttach);
            }
            user.setWarningcommentCollection(attachedWarningcommentCollection);
            Collection<Comment> attachedCommentCollection = new ArrayList<Comment>();
            for (Comment commentCollectionCommentToAttach : user.getCommentCollection()) {
                commentCollectionCommentToAttach = em.getReference(commentCollectionCommentToAttach.getClass(), commentCollectionCommentToAttach.getIdComment());
                attachedCommentCollection.add(commentCollectionCommentToAttach);
            }
            user.setCommentCollection(attachedCommentCollection);
            Collection<Event> attachedEventCollection = new ArrayList<Event>();
            for (Event eventCollectionEventToAttach : user.getEventCollection()) {
                eventCollectionEventToAttach = em.getReference(eventCollectionEventToAttach.getClass(), eventCollectionEventToAttach.getIdEvent());
                attachedEventCollection.add(eventCollectionEventToAttach);
            }
            user.setEventCollection(attachedEventCollection);
            em.persist(user);
            if (idSchedule != null) {
                idSchedule.getUserCollection().add(user);
                idSchedule = em.merge(idSchedule);
            }
            for (Warningevent warningeventCollectionWarningevent : user.getWarningeventCollection()) {
                User oldUserOfWarningeventCollectionWarningevent = warningeventCollectionWarningevent.getUser();
                warningeventCollectionWarningevent.setUser(user);
                warningeventCollectionWarningevent = em.merge(warningeventCollectionWarningevent);
                if (oldUserOfWarningeventCollectionWarningevent != null) {
                    oldUserOfWarningeventCollectionWarningevent.getWarningeventCollection().remove(warningeventCollectionWarningevent);
                    oldUserOfWarningeventCollectionWarningevent = em.merge(oldUserOfWarningeventCollectionWarningevent);
                }
            }
            for (Receivednotification receivednotificationCollectionReceivednotification : user.getReceivednotificationCollection()) {
                User oldUserOfReceivednotificationCollectionReceivednotification = receivednotificationCollectionReceivednotification.getUser();
                receivednotificationCollectionReceivednotification.setUser(user);
                receivednotificationCollectionReceivednotification = em.merge(receivednotificationCollectionReceivednotification);
                if (oldUserOfReceivednotificationCollectionReceivednotification != null) {
                    oldUserOfReceivednotificationCollectionReceivednotification.getReceivednotificationCollection().remove(receivednotificationCollectionReceivednotification);
                    oldUserOfReceivednotificationCollectionReceivednotification = em.merge(oldUserOfReceivednotificationCollectionReceivednotification);
                }
            }
            for (Invitation invitationCollectionInvitation : user.getInvitationCollection()) {
                User oldUserOfInvitationCollectionInvitation = invitationCollectionInvitation.getUser();
                invitationCollectionInvitation.setUser(user);
                invitationCollectionInvitation = em.merge(invitationCollectionInvitation);
                if (oldUserOfInvitationCollectionInvitation != null) {
                    oldUserOfInvitationCollectionInvitation.getInvitationCollection().remove(invitationCollectionInvitation);
                    oldUserOfInvitationCollectionInvitation = em.merge(oldUserOfInvitationCollectionInvitation);
                }
            }
            for (Warningcomment warningcommentCollectionWarningcomment : user.getWarningcommentCollection()) {
                User oldUserOfWarningcommentCollectionWarningcomment = warningcommentCollectionWarningcomment.getUser();
                warningcommentCollectionWarningcomment.setUser(user);
                warningcommentCollectionWarningcomment = em.merge(warningcommentCollectionWarningcomment);
                if (oldUserOfWarningcommentCollectionWarningcomment != null) {
                    oldUserOfWarningcommentCollectionWarningcomment.getWarningcommentCollection().remove(warningcommentCollectionWarningcomment);
                    oldUserOfWarningcommentCollectionWarningcomment = em.merge(oldUserOfWarningcommentCollectionWarningcomment);
                }
            }
            for (Comment commentCollectionComment : user.getCommentCollection()) {
                User oldIdAuthorOfCommentCollectionComment = commentCollectionComment.getIdAuthor();
                commentCollectionComment.setIdAuthor(user);
                commentCollectionComment = em.merge(commentCollectionComment);
                if (oldIdAuthorOfCommentCollectionComment != null) {
                    oldIdAuthorOfCommentCollectionComment.getCommentCollection().remove(commentCollectionComment);
                    oldIdAuthorOfCommentCollectionComment = em.merge(oldIdAuthorOfCommentCollectionComment);
                }
            }
            for (Event eventCollectionEvent : user.getEventCollection()) {
                User oldIdCreatorOfEventCollectionEvent = eventCollectionEvent.getIdCreator();
                eventCollectionEvent.setIdCreator(user);
                eventCollectionEvent = em.merge(eventCollectionEvent);
                if (oldIdCreatorOfEventCollectionEvent != null) {
                    oldIdCreatorOfEventCollectionEvent.getEventCollection().remove(eventCollectionEvent);
                    oldIdCreatorOfEventCollectionEvent = em.merge(oldIdCreatorOfEventCollectionEvent);
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findUser(user.getEmail()) != null) {
                throw new PreexistingEntityException("User " + user + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(User user) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            User persistentUser = em.find(User.class, user.getEmail());
            Schedule idScheduleOld = persistentUser.getIdSchedule();
            Schedule idScheduleNew = user.getIdSchedule();
            Collection<Warningevent> warningeventCollectionOld = persistentUser.getWarningeventCollection();
            Collection<Warningevent> warningeventCollectionNew = user.getWarningeventCollection();
            Collection<Receivednotification> receivednotificationCollectionOld = persistentUser.getReceivednotificationCollection();
            Collection<Receivednotification> receivednotificationCollectionNew = user.getReceivednotificationCollection();
            Collection<Invitation> invitationCollectionOld = persistentUser.getInvitationCollection();
            Collection<Invitation> invitationCollectionNew = user.getInvitationCollection();
            Collection<Warningcomment> warningcommentCollectionOld = persistentUser.getWarningcommentCollection();
            Collection<Warningcomment> warningcommentCollectionNew = user.getWarningcommentCollection();
            Collection<Comment> commentCollectionOld = persistentUser.getCommentCollection();
            Collection<Comment> commentCollectionNew = user.getCommentCollection();
            Collection<Event> eventCollectionOld = persistentUser.getEventCollection();
            Collection<Event> eventCollectionNew = user.getEventCollection();
            List<String> illegalOrphanMessages = null;
            for (Warningevent warningeventCollectionOldWarningevent : warningeventCollectionOld) {
                if (!warningeventCollectionNew.contains(warningeventCollectionOldWarningevent)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Warningevent " + warningeventCollectionOldWarningevent + " since its user field is not nullable.");
                }
            }
            for (Receivednotification receivednotificationCollectionOldReceivednotification : receivednotificationCollectionOld) {
                if (!receivednotificationCollectionNew.contains(receivednotificationCollectionOldReceivednotification)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Receivednotification " + receivednotificationCollectionOldReceivednotification + " since its user field is not nullable.");
                }
            }
            for (Invitation invitationCollectionOldInvitation : invitationCollectionOld) {
                if (!invitationCollectionNew.contains(invitationCollectionOldInvitation)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Invitation " + invitationCollectionOldInvitation + " since its user field is not nullable.");
                }
            }
            for (Warningcomment warningcommentCollectionOldWarningcomment : warningcommentCollectionOld) {
                if (!warningcommentCollectionNew.contains(warningcommentCollectionOldWarningcomment)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Warningcomment " + warningcommentCollectionOldWarningcomment + " since its user field is not nullable.");
                }
            }
            for (Comment commentCollectionOldComment : commentCollectionOld) {
                if (!commentCollectionNew.contains(commentCollectionOldComment)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Comment " + commentCollectionOldComment + " since its idAuthor field is not nullable.");
                }
            }
            for (Event eventCollectionOldEvent : eventCollectionOld) {
                if (!eventCollectionNew.contains(eventCollectionOldEvent)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Event " + eventCollectionOldEvent + " since its idCreator field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (idScheduleNew != null) {
                idScheduleNew = em.getReference(idScheduleNew.getClass(), idScheduleNew.getIdSchedule());
                user.setIdSchedule(idScheduleNew);
            }
            Collection<Warningevent> attachedWarningeventCollectionNew = new ArrayList<Warningevent>();
            for (Warningevent warningeventCollectionNewWarningeventToAttach : warningeventCollectionNew) {
                warningeventCollectionNewWarningeventToAttach = em.getReference(warningeventCollectionNewWarningeventToAttach.getClass(), warningeventCollectionNewWarningeventToAttach.getWarningeventPK());
                attachedWarningeventCollectionNew.add(warningeventCollectionNewWarningeventToAttach);
            }
            warningeventCollectionNew = attachedWarningeventCollectionNew;
            user.setWarningeventCollection(warningeventCollectionNew);
            Collection<Receivednotification> attachedReceivednotificationCollectionNew = new ArrayList<Receivednotification>();
            for (Receivednotification receivednotificationCollectionNewReceivednotificationToAttach : receivednotificationCollectionNew) {
                receivednotificationCollectionNewReceivednotificationToAttach = em.getReference(receivednotificationCollectionNewReceivednotificationToAttach.getClass(), receivednotificationCollectionNewReceivednotificationToAttach.getReceivednotificationPK());
                attachedReceivednotificationCollectionNew.add(receivednotificationCollectionNewReceivednotificationToAttach);
            }
            receivednotificationCollectionNew = attachedReceivednotificationCollectionNew;
            user.setReceivednotificationCollection(receivednotificationCollectionNew);
            Collection<Invitation> attachedInvitationCollectionNew = new ArrayList<Invitation>();
            for (Invitation invitationCollectionNewInvitationToAttach : invitationCollectionNew) {
                invitationCollectionNewInvitationToAttach = em.getReference(invitationCollectionNewInvitationToAttach.getClass(), invitationCollectionNewInvitationToAttach.getInvitationPK());
                attachedInvitationCollectionNew.add(invitationCollectionNewInvitationToAttach);
            }
            invitationCollectionNew = attachedInvitationCollectionNew;
            user.setInvitationCollection(invitationCollectionNew);
            Collection<Warningcomment> attachedWarningcommentCollectionNew = new ArrayList<Warningcomment>();
            for (Warningcomment warningcommentCollectionNewWarningcommentToAttach : warningcommentCollectionNew) {
                warningcommentCollectionNewWarningcommentToAttach = em.getReference(warningcommentCollectionNewWarningcommentToAttach.getClass(), warningcommentCollectionNewWarningcommentToAttach.getWarningcommentPK());
                attachedWarningcommentCollectionNew.add(warningcommentCollectionNewWarningcommentToAttach);
            }
            warningcommentCollectionNew = attachedWarningcommentCollectionNew;
            user.setWarningcommentCollection(warningcommentCollectionNew);
            Collection<Comment> attachedCommentCollectionNew = new ArrayList<Comment>();
            for (Comment commentCollectionNewCommentToAttach : commentCollectionNew) {
                commentCollectionNewCommentToAttach = em.getReference(commentCollectionNewCommentToAttach.getClass(), commentCollectionNewCommentToAttach.getIdComment());
                attachedCommentCollectionNew.add(commentCollectionNewCommentToAttach);
            }
            commentCollectionNew = attachedCommentCollectionNew;
            user.setCommentCollection(commentCollectionNew);
            Collection<Event> attachedEventCollectionNew = new ArrayList<Event>();
            for (Event eventCollectionNewEventToAttach : eventCollectionNew) {
                eventCollectionNewEventToAttach = em.getReference(eventCollectionNewEventToAttach.getClass(), eventCollectionNewEventToAttach.getIdEvent());
                attachedEventCollectionNew.add(eventCollectionNewEventToAttach);
            }
            eventCollectionNew = attachedEventCollectionNew;
            user.setEventCollection(eventCollectionNew);
            user = em.merge(user);
            if (idScheduleOld != null && !idScheduleOld.equals(idScheduleNew)) {
                idScheduleOld.getUserCollection().remove(user);
                idScheduleOld = em.merge(idScheduleOld);
            }
            if (idScheduleNew != null && !idScheduleNew.equals(idScheduleOld)) {
                idScheduleNew.getUserCollection().add(user);
                idScheduleNew = em.merge(idScheduleNew);
            }
            for (Warningevent warningeventCollectionNewWarningevent : warningeventCollectionNew) {
                if (!warningeventCollectionOld.contains(warningeventCollectionNewWarningevent)) {
                    User oldUserOfWarningeventCollectionNewWarningevent = warningeventCollectionNewWarningevent.getUser();
                    warningeventCollectionNewWarningevent.setUser(user);
                    warningeventCollectionNewWarningevent = em.merge(warningeventCollectionNewWarningevent);
                    if (oldUserOfWarningeventCollectionNewWarningevent != null && !oldUserOfWarningeventCollectionNewWarningevent.equals(user)) {
                        oldUserOfWarningeventCollectionNewWarningevent.getWarningeventCollection().remove(warningeventCollectionNewWarningevent);
                        oldUserOfWarningeventCollectionNewWarningevent = em.merge(oldUserOfWarningeventCollectionNewWarningevent);
                    }
                }
            }
            for (Receivednotification receivednotificationCollectionNewReceivednotification : receivednotificationCollectionNew) {
                if (!receivednotificationCollectionOld.contains(receivednotificationCollectionNewReceivednotification)) {
                    User oldUserOfReceivednotificationCollectionNewReceivednotification = receivednotificationCollectionNewReceivednotification.getUser();
                    receivednotificationCollectionNewReceivednotification.setUser(user);
                    receivednotificationCollectionNewReceivednotification = em.merge(receivednotificationCollectionNewReceivednotification);
                    if (oldUserOfReceivednotificationCollectionNewReceivednotification != null && !oldUserOfReceivednotificationCollectionNewReceivednotification.equals(user)) {
                        oldUserOfReceivednotificationCollectionNewReceivednotification.getReceivednotificationCollection().remove(receivednotificationCollectionNewReceivednotification);
                        oldUserOfReceivednotificationCollectionNewReceivednotification = em.merge(oldUserOfReceivednotificationCollectionNewReceivednotification);
                    }
                }
            }
            for (Invitation invitationCollectionNewInvitation : invitationCollectionNew) {
                if (!invitationCollectionOld.contains(invitationCollectionNewInvitation)) {
                    User oldUserOfInvitationCollectionNewInvitation = invitationCollectionNewInvitation.getUser();
                    invitationCollectionNewInvitation.setUser(user);
                    invitationCollectionNewInvitation = em.merge(invitationCollectionNewInvitation);
                    if (oldUserOfInvitationCollectionNewInvitation != null && !oldUserOfInvitationCollectionNewInvitation.equals(user)) {
                        oldUserOfInvitationCollectionNewInvitation.getInvitationCollection().remove(invitationCollectionNewInvitation);
                        oldUserOfInvitationCollectionNewInvitation = em.merge(oldUserOfInvitationCollectionNewInvitation);
                    }
                }
            }
            for (Warningcomment warningcommentCollectionNewWarningcomment : warningcommentCollectionNew) {
                if (!warningcommentCollectionOld.contains(warningcommentCollectionNewWarningcomment)) {
                    User oldUserOfWarningcommentCollectionNewWarningcomment = warningcommentCollectionNewWarningcomment.getUser();
                    warningcommentCollectionNewWarningcomment.setUser(user);
                    warningcommentCollectionNewWarningcomment = em.merge(warningcommentCollectionNewWarningcomment);
                    if (oldUserOfWarningcommentCollectionNewWarningcomment != null && !oldUserOfWarningcommentCollectionNewWarningcomment.equals(user)) {
                        oldUserOfWarningcommentCollectionNewWarningcomment.getWarningcommentCollection().remove(warningcommentCollectionNewWarningcomment);
                        oldUserOfWarningcommentCollectionNewWarningcomment = em.merge(oldUserOfWarningcommentCollectionNewWarningcomment);
                    }
                }
            }
            for (Comment commentCollectionNewComment : commentCollectionNew) {
                if (!commentCollectionOld.contains(commentCollectionNewComment)) {
                    User oldIdAuthorOfCommentCollectionNewComment = commentCollectionNewComment.getIdAuthor();
                    commentCollectionNewComment.setIdAuthor(user);
                    commentCollectionNewComment = em.merge(commentCollectionNewComment);
                    if (oldIdAuthorOfCommentCollectionNewComment != null && !oldIdAuthorOfCommentCollectionNewComment.equals(user)) {
                        oldIdAuthorOfCommentCollectionNewComment.getCommentCollection().remove(commentCollectionNewComment);
                        oldIdAuthorOfCommentCollectionNewComment = em.merge(oldIdAuthorOfCommentCollectionNewComment);
                    }
                }
            }
            for (Event eventCollectionNewEvent : eventCollectionNew) {
                if (!eventCollectionOld.contains(eventCollectionNewEvent)) {
                    User oldIdCreatorOfEventCollectionNewEvent = eventCollectionNewEvent.getIdCreator();
                    eventCollectionNewEvent.setIdCreator(user);
                    eventCollectionNewEvent = em.merge(eventCollectionNewEvent);
                    if (oldIdCreatorOfEventCollectionNewEvent != null && !oldIdCreatorOfEventCollectionNewEvent.equals(user)) {
                        oldIdCreatorOfEventCollectionNewEvent.getEventCollection().remove(eventCollectionNewEvent);
                        oldIdCreatorOfEventCollectionNewEvent = em.merge(oldIdCreatorOfEventCollectionNewEvent);
                    }
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = user.getEmail();
                if (findUser(id) == null) {
                    throw new NonexistentEntityException("The user with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            User user;
            try {
                user = em.getReference(User.class, id);
                user.getEmail();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The user with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Warningevent> warningeventCollectionOrphanCheck = user.getWarningeventCollection();
            for (Warningevent warningeventCollectionOrphanCheckWarningevent : warningeventCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This User (" + user + ") cannot be destroyed since the Warningevent " + warningeventCollectionOrphanCheckWarningevent + " in its warningeventCollection field has a non-nullable user field.");
            }
            Collection<Receivednotification> receivednotificationCollectionOrphanCheck = user.getReceivednotificationCollection();
            for (Receivednotification receivednotificationCollectionOrphanCheckReceivednotification : receivednotificationCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This User (" + user + ") cannot be destroyed since the Receivednotification " + receivednotificationCollectionOrphanCheckReceivednotification + " in its receivednotificationCollection field has a non-nullable user field.");
            }
            Collection<Invitation> invitationCollectionOrphanCheck = user.getInvitationCollection();
            for (Invitation invitationCollectionOrphanCheckInvitation : invitationCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This User (" + user + ") cannot be destroyed since the Invitation " + invitationCollectionOrphanCheckInvitation + " in its invitationCollection field has a non-nullable user field.");
            }
            Collection<Warningcomment> warningcommentCollectionOrphanCheck = user.getWarningcommentCollection();
            for (Warningcomment warningcommentCollectionOrphanCheckWarningcomment : warningcommentCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This User (" + user + ") cannot be destroyed since the Warningcomment " + warningcommentCollectionOrphanCheckWarningcomment + " in its warningcommentCollection field has a non-nullable user field.");
            }
            Collection<Comment> commentCollectionOrphanCheck = user.getCommentCollection();
            for (Comment commentCollectionOrphanCheckComment : commentCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This User (" + user + ") cannot be destroyed since the Comment " + commentCollectionOrphanCheckComment + " in its commentCollection field has a non-nullable idAuthor field.");
            }
            Collection<Event> eventCollectionOrphanCheck = user.getEventCollection();
            for (Event eventCollectionOrphanCheckEvent : eventCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This User (" + user + ") cannot be destroyed since the Event " + eventCollectionOrphanCheckEvent + " in its eventCollection field has a non-nullable idCreator field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Schedule idSchedule = user.getIdSchedule();
            if (idSchedule != null) {
                idSchedule.getUserCollection().remove(user);
                idSchedule = em.merge(idSchedule);
            }
            em.remove(user);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<User> findUserEntities() {
        return findUserEntities(true, -1, -1);
    }

    public List<User> findUserEntities(int maxResults, int firstResult) {
        return findUserEntities(false, maxResults, firstResult);
    }

    private List<User> findUserEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(User.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public User findUser(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(User.class, id);
        } finally {
            em.close();
        }
    }

    public int getUserCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<User> rt = cq.from(User.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
